package com.teng.mylibrary.base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView

/**
 * Description:  ViewBinding基类adapter
 * Author:       liuteng
 * CreateDate:   2021/12/16 10:55
 * UpdateRemark: 更新说明
 */
abstract class BaseVBAdapter<T>(protected var mList: ArrayList<T>) :
    RecyclerView.Adapter<BaseViewHolder>() {

    private var mOnItemClickListener: ((position: Int) -> Unit)? = null
    private var mOnItemLongClickListener: ((position: Int) -> Unit)? = null

    companion object {
        const val TYPE_HEADER = 10
        const val TYPE_NORMAL = 0
        const val TYPE_FOOTER = 20
    }

    private var mHeaderViewLayout = -1
    private var mFooterViewLayout = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        when (viewType) {
            TYPE_FOOTER -> {
                return FooterViewHolder(
                    DataBindingUtil.inflate(
                        LayoutInflater.from(parent.context),
                        getFooterView(),
                        parent,
                        false
                    )
                )
            }
            TYPE_HEADER -> {
                return HeaderViewHolder(
                    DataBindingUtil.inflate(
                        LayoutInflater.from(parent.context),
                        getHeaderView(),
                        parent,
                        false
                    )
                )
            }
        }
        return MainViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                getItemView(viewType),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        //如果是header footer view就直接返回,不需要绑定数据
        if (getItemViewType(position) == TYPE_HEADER) {
            return
        }
        if (getItemViewType(position) == TYPE_FOOTER) {
            return
        }
        val relPostion = getRealPosition(holder)
        holder.itemView.setOnClickListener { mOnItemClickListener?.invoke(relPostion) }
        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.invoke(relPostion)
            true
        }
        onBindHolder(holder, relPostion)
    }

    protected abstract fun getItemView(viewType: Int): Int

    protected abstract fun onBindHolder(holder: BaseViewHolder, position: Int)

    override fun getItemViewType(position: Int): Int {
        if (mHeaderViewLayout != -1 && position == 0) {
            return TYPE_HEADER
        }
        return if (mFooterViewLayout != -1 && position == itemCount - 1) {
            TYPE_FOOTER
        } else TYPE_NORMAL
    }

    override fun getItemCount(): Int {
        var count: Int = mList.size
        if (mHeaderViewLayout != -1) {
            count++
        }
        if (mFooterViewLayout != -1) {
            count++
        }
        return count
    }

    private fun getRealPosition(holder: BaseViewHolder): Int {
        val position: Int = holder.layoutPosition
        return if (mHeaderViewLayout == -1) position else position - 1
    }

    /**
     * Add header view res
     * @param c,layoutId
     */
    open fun setHeaderView(layoutId: Int) {
        if (layoutId < 0) {
            return
        }
        mHeaderViewLayout = layoutId
        notifyItemInserted(0)
    }

    /**
     * Add footer view res
     * @param c,layoutId
     */
    open fun setFooterView(layoutId: Int) {
        if (layoutId < 0) {
            return
        }
        mFooterViewLayout = layoutId
        notifyItemInserted(itemCount - 1)
    }

    /**
     * Get header view
     * @return
     */
    open fun getHeaderView(): Int {
        return mHeaderViewLayout
    }

    /**
     * Get footer view
     *
     * @return
     */
    open fun getFooterView(): Int {
        return mFooterViewLayout
    }


    /**
     * 刷新数据
     * @param newData ArrayList<T>
     */
    fun refreshData(newData: ArrayList<T>) {
        this.mList = newData
        this.notifyDataSetChanged()
    }

    /**
     * 刷新数据
     * @param newData ArrayList<T>
     */
    fun refreshItemData(position: Int) {
        this.notifyItemChanged(position)
    }

    /**
     * 添加数据
     * @param newData ArrayList<T>
     */
    fun addData(addData: ArrayList<T>) {
        this.mList.addAll(addData)
        notifyItemRangeInserted(itemCount, addData.size)
    }

    /**
     * 删减数据
     * @param position Int
     */
    fun removeData(position: Int) {
        this.mList.removeAt(position)
        this.notifyItemRemoved(position)
        this.notifyItemRangeChanged(position, this.mList.size - position)
    }


    fun setOnItemClickListener(onItemClickListener: ((position: Int) -> Unit)) {
        mOnItemClickListener = onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: ((position: Int) -> Unit)) {
        mOnItemLongClickListener = onItemLongClickListener
    }


}